<?php
/**
 * This class manages students in this application
 *
 * @author Dennis
 *
 */
class StudentsController extends AppController{

	var $uses = array('Student');

	/**
	 * (non-PHPdoc)
	 * @see Controller::beforeFilter()
	 */
	function beforeFilter(){
		parent::beforeFilter();
	}

	/**
	 * (non-PHPdoc)
	 * @see AppController::index()
	 */
	public function index(){
		$this->data = $this->Student->find('all');
		if(empty($this->data)){
			parent::doFlash(array('Info'=>'Es existieren keine Studenten'));
		}
	}

	/**
	 * Adds a student
	 */
	function add(){
		if(!empty($this->data)){
			$ok = $this->Student->save($this->data);
			$name = $this->data['Student']['first_name'].' '.$this->data['Student']['family_name'];
			if($ok){
				parent::doFlash(array('Info'=>'Der Student '.$name.' wurde angelegt'));
				$this->redirect(array('controller'=>'students','action'=>'index'));
			}else{
				parent::doFlash(array('Fehler'=>'Der Student '.$name.' konnte nicht angelegt werden. '.$this->Session->error()));
			}
		}
		$this->setListData();
	}


	/**
	 * Sets the standard list data for the drop-down lists
	 */
	function setListData (){

		// get states ---------------------
		$params = array(
			'fields' => array('State.id','State.state_name')
		);
		$states = $this->Student->State->find('list',$params);
		$this->set('states',$states);

		// get companies ---------------------
		$params = array(
			'fields' => array('Company.id','Company.company_name')
		);
		$companies = $this->Student->Company->find('list',$params);
		$this->set('companies',$companies);

		// get courses ---------------------
		$params = array(
			'fields' => array('Course.id','Course.course_name')
		);
		$courses = $this->Student->Course->find('list',$params);
		$this->set('courses',$courses);
	}

	/**
	 * Updates students data for given student id
	 * @param int $id - id for student to update data for
	 */
	function update($id=null){
		if(is_numeric($id)){
			if(!empty($this->data)){
				$this->Student->id = $id;
				$ok = $this->Student->save($this->data);
				$result = $this->Student->findById($id);
				$name = '"'.$result['Student']['full_name'].'"';
				if($ok){
					parent::doFlash(array('Info'=>'Der Student '.$name.' wurde gespeichert'));
					$this->redirect(array('controller'=>'students','action'=>'index'));
				}else{
					parent::doFlash(array('Info'=>'Der Student '.$name.' konnte nicht gespeichert werden. '.$this->Session->error()));
				}
			}
			$this->data = $this->Student->findById($id);
			$this->setListData();
		}else{
			$message = 'Die übergebene '.$id.' ist keine gültige ID';
			parent::doFlash(array('Info'=>$message));
		}
	}

	/**
	 * Deletes a student
	 * @param unknown_type $id
	 */
	function delete($id=null){
		if(is_numeric($id)){
			$result = $this->Student->findById($id);
			if(!empty($result)){
				$ok = $this->Student->delete($id);
				if($ok){
					$name = $result['Student']['first_name'].' '.$result['Student']['family_name'];
					$message = 'Der Student '.$name.' wurde gelöscht';
					parent::doFlash(array('Info'=>$message));
					$this->redirect(array('controller'=>'students','action'=>'index'));
				}else{
					$message = 'Der Student konnte nicht gelöscht werden. '.$this->Session->error();
					parent::doFlash(array('Fehler'=>$message));
				}
			}else{
				$message = 'Der Student konnte nicht gelöscht werden, da die ID nicht vorhanden ist';
				parent::doFlash(array('Fehler'=>$message));
			}
		}else{
			$message = 'Die übergebene '.$id.' ist keine gültige ID';
			parent::doFlash(array('Fehler'=>$message));
		}
	}
}
?>
